package com.example.work_notebook;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by asus on 2018/6/7.
 */

public class NoteBookBaseAdapter extends RecyclerView.Adapter<NoteBookBaseAdapter.ViewHolder>{
    private List<Notes> mNotes;
    private Context mContext;
    static class ViewHolder extends  RecyclerView.ViewHolder{
        View noteView;
        TextView Nname;
        TextView Nitems;
        TextView Ntimes;
        public ViewHolder(View itemView) {
            super(itemView);
            noteView = itemView;
            Nname = (TextView) itemView.findViewById(R.id.item_name);
            Nitems = (TextView) itemView.findViewById(R.id.item_items);
            Ntimes = (TextView) itemView.findViewById(R.id.item_time);
        }
    }
    public NoteBookBaseAdapter(Context mContext,List<Notes> mNotes){
        this.mContext = mContext;
        this.mNotes = mNotes;
    }
    @Override
    public NoteBookBaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
       ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(NoteBookBaseAdapter.ViewHolder holder, final int position) {
      Notes note = mNotes.get(position);
      String title = note.getName();
      String data = note.getItems();
        if(title.length()>10){
            title = title.substring(0,10)+"...";
        }
        if (data.length()>8) {
            data = data.substring(0,6)+"...";
        }
      holder.Nname.setText(title);
      holder.Nitems.setText(data);
      holder.Ntimes.setText(note.getTimes());
      holder.noteView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(mContext,EditActivity.class);
              intent.putExtra("ID",mNotes.get(position).getObjId());
              intent.putExtra("TITLE",mNotes.get(position).getName());
              intent.putExtra("DATA",mNotes.get(position).getItems());
              mContext.startActivity(intent);
          }
      });
    }

    @Override
    public int getItemCount() {
        return mNotes.size();
    }
}
